class OrderItem < ActiveRecord::Base
  belongs_to :order
  belongs_to :position
  belongs_to :inside_position
  
  #交换指数
  def get_exchange_index(position,order,spoiler)
    self.recombine = 0.0
    self.cut_index = 0.0
    part = position.part
    car_size = order.car_size
    uninstall = Uninstall.find(:first, :conditions => "part = '#{part}' and car_size = '#{car_size}'")
    cut = Cut.find(:first, :conditions => "part = '#{part}' and car_size = '#{car_size}'")
    if position.dismantle == true
      self.work_judge = "交換"
      self.task_type = "単体"
      self.uninstall_index = uninstall.disassemble_time
      self.assemble = uninstall.assemble_time
      if spoiler == "T"
        self.assemble = self.assemble + 0.7
      end
      if ["フロントドア","リアドア","バックドア"].find{|item| item == position.part}
        self.recombine = uninstall.recombine_time
      end
    else
      self.work_judge = "カットから"
      self.metal_putty_index = cut.metal_putty_index
      self.cut_index = cut.cut_index
      if position.part == "フロントパネル"
        self.uninstall_index = uninstall.disassemble_time
        self.assemble = uninstall.assemble_time
      end
    end
    self.metal_shape = "なし"
    #塗装範囲="交換"  ??
    
  end
  
  #钣金指数
  def get_metal_index(metal_difficulty)
    area_x = self.area_x
    area_y = self.area_y
    
    area = area_x * area_y
    #in situation of line
    if self.damage_shape == "線状"
      
      if self.damage_degree == "塗膜のみ"
        self.metal_index = 0.02 * area_x + 0.2
        if self.metal_index < 0.6
          self.metal_index = 0.6
        end
      end
      
      if self.damage_degree == "へこみ５ｍｍ以下"
        self.metal_index = 0.0015 * area_x * area_x - 0.055 * area_x + 1.3
        if self.metal_index < 0.8
          self.metal_index = 0.8
        end
      end
      
      if self.damage_degree == "へこみ５ｍｍ以上"
        self.metal_index = -0.8 / 10000 * area_x * area_x * area_x + 0.0065 * area_x * area_x - 0.1167 * area_x + 1.4
        if self.metal_index < 0.8
          self.metal_index = 0.8
        end
      end
      
    end
    # in situation of area
    
    if self.damage_shape == "面状"
      
      if self.damage_degree == "塗膜のみ"
        self.metal_index = -0.4 * 10 ** -6 * area * area + 1.5 * 10 ** -3 * area + 0.45 
      end
      
      if self.damage_degree == "へこみ５ｍｍ以下"
        self.metal_index = -0.2 * 10 ** -6 * area * area + 1.4 * 10 ** -3 * area + 0.66
      end
      
      if self.damage_degree == "へこみ５ｍｍ以上"
        self.metal_index = -1.0 * 10 ** -6 * area * area + 3.2 * 10 ** -3 * area + 0.49
      end
    end
    
    self.metal_index = f_round(metal_index,2)
    self.metal_putty_index = self.metal_index - self.metal_index / 5.0
    self.metal_putty_index = f_round(self.metal_putty_index,3)
    self.putty_index = self.metal_index + 0.2
    self.metal_index = self.metal_index / 5.0
    if self.metal_index < 0.5
      self.metal_index = self.metal_index + 0.166
    end
    self.metal_index = self.metal_index * metal_difficulty
    
  end
  
  #パテ指数
  def get_putty_index(order)
    area_x = self.area_x
    area_y = self.area_y
    area = area_x * area_y
    
    if self.work_judge == "交換" 
      self.putty_index = 0.0
      self.surface_index = 0.0
    elsif self.work_judge == "カットから"
      self.surface_index = 0.0
      if order.car_size == "軽自動車"
        self.putty_index = 1.0
      elsif order.car_size == "普通車"
        self.putty_index = 1.0
      else
        self.putty_index = 1.0
      end
    else
      
      if self.damage_shape == "線状"
        if self.damage_degree == "へこみ５ｍｍ以上" and area_x > 20
          self.putty_index = -0.0001 * area_x * area_x * area_x + 0.008823 * area_x * area_x - 0.186115 * area_x + 1.248
        else
          self.putty_index = 0.0083 * area_x + 0.0833
        end
      end
      
      if self.damage_shape == "面状"
        if self.damage_degree == "へこみ５ｍｍ以上"
          self.putty_index = -0.000002 * area * area + 0.0035 * area + 0.1667
        else
          self.putty_index = 0.0000003 * area * area + 0.0001 * area + 0.15
        end
      end
      
      self.putty_index = f_round(self.putty_index,3)
      self.surface_index = self.putty_index * 0.5
      if self.damage_degree == "塗膜のみ"
        self.putty_index = 0.0
      else
        self.putty_index = self.putty_index + 0.33
      end
      self.surface_index = self.surface_index + 0.33
      self.surface_index = f_round(self.surface_index,3)
      
    end
    
  end
  
  #塗装指数
  def get_paint_index(position,order)
    
    if self.work_judge == "交換"
      
      if ["ボンネット","フェンダー","フロントドア","リアドア","トランク","バックドア","フロントエンドパネル","エンドパネル"].find{|item| item == position.part}
        self.work_judge = "交換(足付けから)"
        #self.surface_index = Painting.find(:first, :conditions => "part = '#{position.part}' and car_size = '#{order.car_size}' and paint_type = '#{order.paint_type}'").paint_index
        self.paint_index = 2.0 * Painting.find(:first, :conditions => "part = '#{position.part}' and car_size = '#{order.car_size}' and paint_type = '#{order.paint_type}'").paint_index
      end
      
      self.metal_index = 0.0
      self.metal_putty_index = 0.0
      self.putty_index = 0.0
      self.surface_index = 0.0
    else
      self.paint_index = (self.putty_index/2.0) * 4.0
    end
    
    if self.work_judge == "カットから"
      self.paint_index = Painting.find(:first, :conditions => "part = '#{position.part}' and car_size = '#{order.car_size}' and paint_type = '#{order.paint_type}'").paint_index
      self.surface_index = self.paint_index / 9.0 * 2.0
      self.paint_index = self.paint_index - self.surface_index
      self.metal_index = 0.0
      self.polish = self.paint_index / 4.0
    elsif self.work_judge == "サフから"
      self.paint_index = self.surface_index / 2.0 * 3.0
      self.metal_putty_index = 0.0
      self.metal_index = 0.0
      self.cut_index = 0.0
      self.recombine = 0.0
      self.polish = self.paint_index / 4.0
    elsif self.work_judge == "塗装パテから"
      self.metal_putty_index = 0.0
      self.metal_index = 0.0
      self.cut_index = 0.0
      self.recombine = 0.0
      self.polish = self.paint_index / 4.0
    elsif self.work_judge == "鈑金パテから"
      self.metal_index = 0.0
      self.cut_index = 0.0
      self.recombine = 0.0
      self.polish = self.paint_index / 4.0
    elsif self.work_judge == "凹み修正から"
      self.cut_index = 0.0
      self.recombine = 0.0
      self.polish = self.paint_index / 4.0
    else
      self.polish = self.paint_index / 4.0
    end

  end

  #工数算出
  def calculation 
    
  end
  
  #get the round of data
  def f_round(data, n)
    y = 1
    n.times do
      y = y * 10
    end
    result = (data * y).round / (y * 1.0)
    return result
  end


end
